//
//  ICChatDetailCell.m
//  ICome
//
//  Created by ENN on 16/4/21.
//  Copyright © 2016年 iCom. All rights reserved.
//

#import "ICChatDetailCell.h"
#import "ICCommonItem.h"
#import "ICSettingNoneItem.h"
#import "ICSettingArrowItem.h"
#import "ICSettingSwitchItem.h"
#import "ICGroup.h"
#import "ICMessageDatabase.h"

@interface ICChatDetailCell ()

@property (nonatomic, strong) UISwitch *switchView;

@property (nonatomic, weak) UILabel *subTitleLabel;

@end

@implementation ICChatDetailCell


+ (instancetype)cellWithTableView:(UITableView *)tableView
{
    static NSString *ID = @"DetailCell";
    
//    ICChatDetailCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
//    if (nil == cell) {
        ICChatDetailCell *cell = [[ICChatDetailCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
//    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}


//- (void)layoutSubviews
//{
//    [super layoutSubviews];
//    
//    CGRect adjustedFrame = self.accessoryView.frame;
//    adjustedFrame.origin.y += 12.0f;
//    self.accessoryView.frame = adjustedFrame;
//}


- (void)setItem:(ICCommonItem *)item
{
    _item    = item;
    
    [self setUpData];
    
    [self setUpAccessoryView];
}

- (void)setUpData
{
    self.textLabel.text = _item.title;
    self.textLabel.font = [UIFont systemFontOfSize:16];
    
    if (_item.detail) {
        self.subTitleLabel.text = _item.detail;
        [_subTitleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(self.mas_right).offset(-35);
            make.width.mas_equalTo(118);
            make.height.mas_equalTo(20);
            make.centerY.equalTo(self.mas_centerY);
        }];
    }
}

- (void)setUpAccessoryView
{
    if ([_item isKindOfClass:[ICSettingArrowItem class]]) {
        self.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    } else if ([_item isKindOfClass:[ICSettingSwitchItem class]]) {
        self.accessoryView = self.switchView;
    } else if ([_item isKindOfClass:[ICSettingNoneItem class]]) {
        self.accessoryView = nil;
    } else {
        self.accessoryView = nil;
    }
}

- (void)topStatusChanged:(UISwitch *)swtch
{
    ICGroup *group = (ICGroup *)_item.parameter;
    if (swtch.on) {
        [ICMessageDatabase makeGroupTop:group isTop:YES lastMsgTime:[ICMessageHelper currentMessageTime]];
    } else {
        [ICMessageDatabase makeGroupTop:group isTop:NO lastMsgTime:[ICMessageHelper currentMessageTime]];
    }
}

- (void)busyStatusChanged:(UISwitch *)swtch
{
    NSString *gId  = (NSString *)_item.parameter;
    
    [self groupBusy:gId isBusy:swtch.on];
    
}

// 免打扰设置
- (void)groupBusy:(NSString *)gId
           isBusy:(BOOL)isBusy
{
    NSDictionary *param = @{
                            @"gId" :gId,
                            @"eId" :[ICUser currentUser].eId,
                            @"busy":[NSNumber numberWithBool:isBusy]
                            };
    [[ICSocketRequestManager sharedInstance] groupBusyWithObject:param Success:^(id response) {
        [ICMessageDatabase changeGroupBusy:gId isBusy:isBusy];// 本地状态
        NSLog(@"ICGroupBusyAPI success");
    } Failure:^(NSError *error, id other) {
         NSLog(@"ICGroupBusyAPI request error");
    }];
}

#pragma mark - Getter

- (UISwitch *)switchView
{
    if (_switchView == nil) {
        if ([_item.parameter isKindOfClass:[ICGroup class]]) {
            _switchView    = [[UISwitch alloc] initWithFrame:CGRectMake(0, 0, 40.5, 22)];
            ICGroup *group = (ICGroup *)_item.parameter;
            BOOL isTop     = [ICMessageDatabase isTopWithGroupId:group.gId];
            _switchView.on = isTop;
            [_switchView addTarget:self action:@selector(topStatusChanged:) forControlEvents:UIControlEventValueChanged];
            _switchView.onTintColor = NE_BACKGROUND_COLOR;
        } else if ([_item.parameter isKindOfClass:[NSString class]]) {
            _switchView    = [[UISwitch alloc] initWithFrame:CGRectMake(0, 0, 40.5, 22)];
            NSString *gId  = (NSString *)_item.parameter;
            BOOL isBusy    = [ICMessageDatabase isBusyWithGroupId:gId];
            _switchView.on = isBusy;
            [_switchView addTarget:self action:@selector(busyStatusChanged:) forControlEvents:UIControlEventValueChanged];
            _switchView.onTintColor = NE_BACKGROUND_COLOR;
        }
       
    }
    return _switchView;
}

- (UILabel *)subTitleLabel
{
    if (!_subTitleLabel) {
        UILabel *label = [[UILabel alloc] init];
        label.font     = ICFont(14 * Level);
        [self.contentView addSubview:label];
        _subTitleLabel = label;
        label.textColor = IColor(177, 178, 177);
        label.textAlignment = NSTextAlignmentRight;
    }
    return _subTitleLabel;
}



@end
